package com.smmisia.knife4j.config;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(Knife4jProperties.class)
@ConditionalOnProperty(prefix = "knife4j", name = "enable", havingValue = "true", matchIfMissing = true)
public class Knife4jAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public OpenAPI customOpenAPI(Knife4jProperties properties) {
        return new OpenAPI()
                .info(new Info()
                        .title("API Documentation")
                        .version("1.0"));
    }

    @Bean
    @ConditionalOnMissingBean
    public GroupedOpenApi defaultApi(Knife4jProperties properties) {
        return GroupedOpenApi.builder()
                .group("default")
                .pathsToMatch(properties.getSpringDoc().getPathsToMatch())
                .packagesToScan(properties.getSpringDoc().getPackagesToScan())
                .build();
    }
}
